﻿#include"head.h"
#include"funcs.h"
#include"nodes.h"

unordered_map<char, node*> buf;
vector<string> Rules;

char ending[] = "ii/*-+()=";

vector<string> split(const string& s, char delimiter) {

    vector<string> tokens;
    string token;
    istringstream tokenStream(s);

    while (getline(tokenStream, token, delimiter)) {
        tokens.push_back(token);
    }
    return tokens;
}

void initBuf() {
    for (auto &item : Rules) {
        auto temp = new node(item);
        buf.insert(make_pair(item[0], temp));
    }
}

void getRuls() {

    ifstream in(rulsFile);
    while (!in.eof()) {
        string temp;
        in >> temp;
        Rules.push_back(temp);
    }

    Rules.pop_back();

    initBuf();
}

void initMap() {
    for (auto &item : buf) {
        item.second->findFirst();
        item.second->findFollow();
    }
    return;
}

bool isEnding(char var) {
    for (char ch : ending) {
        if (var == ch) return 1;
    }

    return 0;
}

void setUnion(unordered_set<int>& dest, unordered_set<int>& sorce, bool exceptEpi) {
    for (auto item : sorce) {
        if (exceptEpi && item == Epi) continue;
        dest.insert(item);
    }
}

int inString(string& str, char var, bool from1) {
    for (int i = from1; i < str.size(); i++) {
        if (str[i] == var) {
            return i;
        }
    }

    return -1;
}

int toInt(char var) {
    return var - '0';
}

void pushToStack(string& str, stack<char>& stk) {
    for (int i = str.size() - 1; i >= 0; i--) {
        stk.push(str[i]);
    }
}